/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import diagram.Graphical;
import diagram.Node;
import diagram.connections.TriggerConnection;
import diagram.handles.ConnectHandle;
import diagram.handles.TriggerHandle;
import diagram.handles.Handle;
import java.awt.event.ActionEvent;
import java.util.Timer;
import javax.swing.AbstractAction;
import listeners.TimerListener;
import model.DiagramModel;

/**
 *
 * @author Jelle
 */
public class DetachAction extends AbstractAction {

    private DiagramModel model;
    private int modus;
    private Graphical g;
    private Timer t;

    public DetachAction(Graphical g, DiagramModel model) {
        this.model = model;
        this.g = g;
        if (g.getClass().getName().equals("diagram.handles.TriggerHandle")) {
            modus = 0;
        } else if (g.getClass().getName().equals("diagram.handles.ResizeHandle")) {
            modus = 1;
        } else if (g.getClass().getName().equals("diagram.Node")) {
            modus = 2;
        }
    }

    public void actionPerformed(ActionEvent e) {
        switch (modus) {
            case 0:
                System.out.println("ModifyAction mode 0 (detach trigger)");
                TriggerHandle c = (TriggerHandle) g;
                for (Graphical gs : model.getObjects("handle")) {
                    Handle h = (Handle) gs;
                    if (h.getClass().getName().equals("diagram.handles.TriggerHandle")) {
                        TriggerHandle ch = (TriggerHandle) h;
                        if (ch.equals(c)) {
                            for (Graphical gg : model.getObjects("triggerconnection")) {
                                TriggerConnection tc = (TriggerConnection) gg;
                                if (tc.getTarget().equals(ch.getLocation())) {
                                    for (Graphical ge : model.getObjects("node")) {
                                        Node ng = (Node) ge;
                                        tc.print();
                                        if (ng.getUpperRight().getX()==tc.getSource().getX()) {
                                            ConnectHandle hc = new ConnectHandle(ng);
                                            
                                            System.out.println("gonne shrink");
                                            tc.shrink(hc);
                                            
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case 1:
                System.out.println("ModifyAction mode 1");

                break;
            case 2:
                System.out.println("ModifyAction mode 2");

                break;

        }
    }
}
